package com.ts.common.utils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.Map;

/**
 * @author bobby
 * @version V1.0
 * @Package com.ts.common.utils
 * @date 2022-11-15 17:19
 * @Copyright © 2019-2039 广州天迅网络科技有限公司
 */
public abstract class PageInfos<T> {
    Map<String, Object> params;

    List<T> list;

    public PageInfos(Map<String, Object> params) {
        this.params = params;
    }

    public abstract List<T> inject(Map<String, Object> params);

    public PageUtils query() {
        if (params.get("offset") != null && params.get("limit") != null) {
            int offset = Integer.parseInt(params.get("offset").toString());
            int limit = Integer.parseInt(params.get("limit").toString());
            params.remove("offset");
            params.remove("limit");
            PageHelper.startPage(offset / limit + 1, limit);
        }
        list = inject(this.params);
        PageInfo<T> pageInfo = new PageInfo(list);
        return new PageUtils(pageInfo.getList(), (int) pageInfo.getTotal());
    }
}
